home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Add-Ons / After Dark / InvertShapes / InvertShapes.c < prev    next >
Encoding:
Text File  |  1994-05-01  |  6.5 KB  |  241 lines  |  [TEXT/KAHL]

  1.  
  2. // ver 1.0.1 - updated the RangedRdm() routine
  3. //         - fixed where I had called it badly
  4.  
  5. #include <QuickDraw.h>
  6. #include <Memory.h>
  7. #include <Resources.h>
  8.  
  9. #include <QDoffscreen.h>
  10.  
  11. #include "GraphicsModule_Types.h"
  12. #include "Sounds.h"
  13.  
  14. unsigned short RangedRdm( unsigned short min, unsigned short max );
  15.  
  16. // these are the functs that need defined ...
  17. OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params);
  18. OSErr DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  19. OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  20. OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  21. OSErr DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  22.  
  23. // extra ones
  24. OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  25. OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  26.  
  27.  
  28.  
  29. // this is borrowed from the example code from Bouncing Ball ...
  30. /* some macros to simplify synchronizing to the vertical retrace. */
  31. #define SynchFlag(m) (params->monitors->monitorList[m].synchFlag)
  32. #define SynchVBL(m) synchFlag = &SynchFlag(m); *synchFlag = false; while(!*synchFlag);
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //////////////////////////////////////////////////////////////////////////////////////
  40. // this is the first funct called by AD ... we need to allocate and initialize here
  41. OSErr
  42. DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  43.     
  44.     // Randomize...
  45.     params->qdGlobalsCopy->qdRandSeed = TickCount();
  46.  
  47.     return noErr;
  48. }
  49.  
  50. //////////////////////////////////////////////////////////////////////////////////////
  51. // the screen saver has been awakened! time to ditch the storage and wave goodbye
  52. OSErr 
  53. DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  54.     return noErr;
  55. }
  56.  
  57.  
  58.  
  59. //////////////////////////////////////////////////////////////////////////////////////
  60. // make the screen go black
  61. OSErr
  62. DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  63.  
  64.     if (params->controlValues[2])
  65.         FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
  66.     return noErr;
  67.  
  68. }
  69.  
  70. //////////////////////////////////////////////////////////////////////////////////////
  71. // this is the workhorse routine. It does the continual screen work to make
  72. // this screen saver what it is.
  73. OSErr 
  74. DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  75.     long    when;
  76.     Rect    r, s;
  77.     short val;
  78.     static theMonitor = 0;
  79.     static theCountdown = 0;
  80.     long color = blackColor;
  81.     Boolean doColor;
  82.  
  83.  
  84. //    if (  !(Random()%10) ) {    // how fast these happen
  85.     if (!theCountdown) {
  86.         short wheretodim;
  87.         wheretodim = (params->controlValues[theMonitor] * 255 ) / 100;
  88.         
  89.         params->brightness -= 1;    //5;
  90.         
  91.         if (params->brightness < wheretodim) 
  92.             params->brightness = wheretodim;
  93.     }
  94.  
  95.     
  96.     // if we have color and this monitor is not B&W, set the flag
  97.     doColor = params->colorQDAvail &&                                    // we have color?
  98.         params->monitors->monitorList[theMonitor].curDepth > 1 &&        // screen has some depth?
  99.         params->controlValues[3];                                        // check box checked?
  100.         
  101.     if (doColor) {
  102.         short pm;
  103.         
  104.         color = RangedRdm(0, 7);    // 8 predefined colors
  105.         switch (color) {
  106.             case 1: ForeColor(blackColor); break;
  107.             case 2: ForeColor(whiteColor); break;
  108.             case 3: ForeColor(redColor); break;
  109.             case 4:    ForeColor(greenColor); break;
  110.             case 5:    ForeColor(blueColor); break;
  111.             case 6:    ForeColor(cyanColor); break;
  112.             case 7:    ForeColor(magentaColor); break;
  113.             default:ForeColor(yellowColor); break;
  114.         }
  115.         // should I change the backcolor here also ?
  116.         do
  117.             pm = RangedRdm( patCopy, notPatBic);
  118.         while (pm == patBic);    // anything but picBic - that makes it white
  119.         PenMode( pm );
  120.     }
  121.     
  122.     theCountdown++;
  123.     if (theCountdown >= 5) theCountdown = 0;
  124.     
  125.     
  126.     r.left = RangedRdm( params->monitors->monitorList[theMonitor].bounds.left, 
  127.             params->monitors->monitorList[theMonitor].bounds.right);
  128.             
  129.     r.right = RangedRdm( params->monitors->monitorList[theMonitor].bounds.left,
  130.             params->monitors->monitorList[theMonitor].bounds.right);
  131.     
  132.     r.top = RangedRdm( params->monitors->monitorList[theMonitor].bounds.top, 
  133.             params->monitors->monitorList[theMonitor].bounds.bottom);
  134.             
  135.     r.bottom = RangedRdm( params->monitors->monitorList[theMonitor].bounds.top,
  136.             params->monitors->monitorList[theMonitor].bounds.bottom);
  137.  
  138.     if (++theMonitor >= params->monitors->monitorCount)
  139.         theMonitor=0;
  140.     
  141.     val = params->controlValues[1];
  142.     // first menu item is RANDOM!!!!
  143.     if (val < 2) 
  144.         val = RangedRdm(2, 5);        // 4 choices now
  145.         
  146.     switch( val ) {
  147.     case 2:    
  148.         if (doColor)
  149.             PaintRect( &r);
  150.         else
  151.             InvertRect( &r );            
  152.         break;
  153.     case 3:    
  154.         if (doColor)
  155.             PaintArc( &r, RangedRdm(0, 360), RangedRdm(0, 360) );        
  156.         else
  157.             InvertArc( &r, RangedRdm(0, 360), RangedRdm(0, 360) );        
  158.         break;
  159.     case 4:    
  160.         if (doColor)
  161.             PaintOval( &r);        
  162.         else
  163.             InvertOval( &r );        
  164.         break;
  165.     case 5:    {
  166.             short w, h;
  167.             
  168.             w = r.right - r.left;
  169.             if (w <= 2) 
  170.                 w = 2;
  171.             else 
  172.                 w = RangedRdm(2, w);
  173.             h = r.bottom - r.top;
  174.             if (h <= 2) 
  175.                 h = 2;
  176.             else 
  177.                 h = RangedRdm(2, h);
  178.             if (doColor)
  179.                 PaintRoundRect( &r, w, h);
  180.             else
  181.                 InvertRoundRect( &r, w, h);    
  182.         }
  183.         break;
  184.     default:    
  185.         SysBeep(0);
  186.         InvertRect( &r );    
  187.         break;
  188.     }
  189.     
  190.     return noErr;
  191.     
  192. }
  193. //////////////////////////////////////////////////////////////////////////////////////
  194. // this is called when they click on something in the control panel
  195. OSErr 
  196. DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
  197.  
  198.     return noErr;
  199. }
  200.  
  201.  
  202.  
  203. OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params)
  204. {
  205.     // I tried playing with params here and they don't seem instantiated,
  206.     // so don't play too much in this routine
  207.     return noErr;
  208. }
  209.  
  210.  
  211.  
  212. // this is from the Think C reference code example ...
  213. unsigned short RangedRdm( unsigned short min, unsigned short max )
  214. /* assume that min is less than max */
  215. {
  216.     // uh ... not this isn't quite right - it's between 0 and 65535, not 65536
  217.     unsigned    qdRdm;    /* treat return value as 0-65536 */
  218.     long    range, t;
  219.     
  220.     // just to be safe, I'll put this here
  221.     if (min > max) DebugStr("\pMin greater then Max in RangedRdm");
  222.     
  223.     qdRdm = Random();
  224.     range = max - min;
  225.     // max - min gives us the the difference between max and min ... that is 
  226.     // not inclusive. It gives us { min <= range < max }
  227.     // so we never see that max number!!
  228.     range++;
  229.     t = ((long)qdRdm * range) / 65536;     /* now 0 <= t <= range */
  230.     return( t+min );
  231. }
  232.  
  233.  
  234.  
  235. OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params)
  236. {
  237.     // I set the proper resource, but I never get this call!!!???
  238.     SysBeep(0);        // lets see if we ever get this verdammnt routine called.
  239.     return noErr;
  240. }
  241.